<template>
	<view class="content">
		<image class="logo" src="@/static/logo.png"></image>
		<view class="text-area">
			<text class="title">废弃油脂移动端\n\n\n\n</text>
		</view>

		<view class="text-area" v-if="checkPermi(['system:qrcode'])">
			<button @click="scanQRcodes">扫码</button>
		</view>
	</view>
</template>

<script>
	import { checkPermi, checkRole } from "@/utils/permission"; // 权限判断函数

	export default {
		data() {
			return {}
		},
		methods: {
			checkPermi,
			checkRole,
			scanQRcodes() {
				// 测试
				// uni.navigateTo({
				// 	url: `/pages/record/index?merchantId=3`
				// });

				// 生产
				uni.scanCode({
					success: (res) => {
						if (res.result) {
							uni.showToast({
								title: '扫码成功',
								//将值设置为 success 或者直接不用写icon这个参数
								icon: 'none',
								//显示持续时间为 2秒
								duration: 2000
							})
							setTimeout(() => {
								if (res.result && !isNaN(res.result)) {
									uni.navigateTo({
										url: `./record/index?merchantId=${res.result}`
									});
								} else {
									uni.showToast({
										title: '未知商户',
										//将值设置为 success 或者直接不用写icon这个参数
										icon: 'none',
										//显示持续时间为 2秒
										duration: 2000
									})
								}
							}, 2000);

							console.log('扫描结果：', res.result);
						} else {
							uni.showToast({
								title: '扫描失败',
								icon: 'none'
							});
						}
					},
					fail: (err) => {
						if (err.errMsg.includes('cancel')) {
							uni.showToast({
								title: '扫描已取消',
								icon: 'none'
							});
						} else {
							uni.showToast({
								title: '调用相机失败',
								icon: 'none'
							});
						}
					}
				});
			}
		}
	}
</script>

<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
